from selenium import webdriver
from page_objects import PageElement, PageObject
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep


class CommonPage:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(5)
        self.wait = WebDriverWait(self.driver, 30)
        self.driver.maximize_window()
        self.driver.get('https://time.geekbang.org/')

    def login(self, name, pw):
        # 弹窗关闭按钮
        login_1 = self.driver.current_window_handle
        self.driver.switch_to.window(login_1)
        self.driver.find_element(By.CSS_SELECTOR, '._1TwiVsuM_0').click()
        self.driver.find_element(By.LINK_TEXT, '登录').click()
        login_w = self.driver.current_window_handle
        self.driver.switch_to.window(login_w)
        self.driver.find_element(By.LINK_TEXT, '密码登录').click()
        self.driver.find_element(By.CSS_SELECTOR, '[name="cellphone"]').click()
        self.driver.find_element(By.CSS_SELECTOR, '[name="password"]').send_keys(pw)
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, '[name="cellphone"]').send_keys(name)
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, '.Button_button_3onsJ').click()
        return self.driver





